package org.example.algorithm.array;

import java.util.ArrayList;
import java.util.List;

public class CombineSolution {

    public static void main(String[] args) {
        CombineSolution solution = new CombineSolution();
        List<List<Integer>> res = solution.combine(4, 2);
        System.out.println(res);
    }
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        dfs(n, k, list, res);
        return res;
    }

    private void dfs(int n, int k, List<Integer> list, List<List<Integer>> res) {
        if (n == 0 || k == 0) {
            if (k == 0) {
                res.add(new ArrayList<>(list));
            }
            return;
        }
        if (n < k) {
            return;
        }
        if (n > k) {
            dfs(n-1, k, list, res);
        }
        list.add(n);
        dfs(n-1, k-1, list, res);
        list.remove(list.size()-1);
    }
}
